#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tile import *
import playermodule
# MAIN TYPES :
SHIP = 1
SPACE = 2

# stellar map tile types
SPACE = 0
ASTEROIDS_FIELD = 1
PLANET = 2

# Ship tile types

HALL = 100
SINGLEWALL = 101
DOUBLEWALL = 102
HALL = 103
FULLWALL = 104
EMPTY = 105

REACTOR = 200
RADAR = 201
CRYO = 202
DRIVE = 203
SHIELDCONTROL = 204
ATTACKCONTROL = 205
WAREHOUSE = 206
SHIELD = 207
WEAPONSLOT = 208
CORRIDOR = 209

# Hex positions (orientations) or Hex border
NE=5
SE=4
S =3
SO=2
NO=1
N =0

oppositePos = {S:N,SE:NO,SO:NE,
                N:S,NO:SE,NE:SO}

# In order to get the neighbour of an Hex which is close to the given border
# Since even an odd x coordinates modify the numerotation of neighbours, we need
# 2 dict in order to do this.

Pos2NeighbourEven = {N : (0,-1),
                     NO : (-1,-1),
                     SO : (-1,0),
                      S : (0,1),
                      SE : (1,0),
                     NE : (1,-1)
                     }
                              
Pos2NeighbourOdd = {N : (0,-1),
                     NO : (-1,0),
                     SO : (-1,1),
                      S : (0,1),
                      SE : (1,1),
                     NE : (1,0)
                     }



def getOppositePos(pos):
    return oppositePos[pos]


class basicHex(object):
    # coord <=> (x,y) where x and y are integers
    """ pos :
  0
1   5
2   4
  3
    """
    maintype = SHIP
    description = "Basic Hex"
    
    def __init__(self,coord,pos,skin,passable=[N,NE,NO,S,SE,SO],players=None,owner = None):
        
        self.tile = mapTiles.getTile(skin)
        self.minimapTile = minimapTiles.getTile(skin)
        self.skin = skin
        self.pos = pos # Hex orientation
        self.coord = coord # coordinates of the hex as integers
        self.owner = owner # owner of the hex when applicable
        self.type = EMPTY # type of the hex
        self.players = players # List of players in order to init status
        self.currentPlayer = players[0] #Current player 
                             
                                #The different status displayed will be those of this player
                             
	self.globFlags = []
        
        self.status = {} #Status of this hex for each player (visible, hidden, last seen N turns ago ...)
        self.units = [] # List of units in the hex (in ships the list should not exceed one unit)

        self.passable = passable # All parts of the hex that are passables
                                 #(These are given assuming that the hex has not been turned and thus that pos=0)

        self.text = ""     
        for player in players:
            self.addPlayerStatus(player)
        
        self.updatePassable()
    
    def clone(self):
        if type(self)!=basicHex:
            clone = type(self)(self.coord,self.pos,self.skin,self.players,self.owner)
            clone.currentPlayer = self.currentPlayer
            return clone

    def refreshSkin(self):
        self.tile = mapTiles.getTile(self.skin)
        self.minimapTile = minimapTiles.getTile(self.skin)

    def getCoord(self):
        return self.coord

    def setCoord(self,coord):
        self.coord = coord

    def setPos(self,pos):
        self.pos = pos

    def addPos(self,pos):
        self.pos = (self.pos + pos + 6) % 6
          
    def updatePassable(self):
        tmppassable = []
        for direction in self.passable:
            direction+=self.pos
            direction = direction % 6
            tmppassable.append(direction)
        self.passable = tmppassable

    def addPlayerStatus(self,player):
        if not self.status.has_key(player):
            self.status[player]=[]

    def addFlag(self,player,flag):
        if not self.isFlag(player,flag):
            self.status[player].append(flag)

    def addGlobFlag(self,flag):
        if not self.isGlobFlag(flag):
            self.globFlags.append(flag)
            
    def rmGlobFlag(self,flag):
        if self.isGlobFlag(flag):
            self.globFlags.remove(flag)

    def rmFlag(self,player,flag):
        if flag in self.status[player]:
            self.status[player].remove(flag)

    def isFlag(self,player,flag):
        if flag in self.status[player]:
            return True
        else:
            return False

    def isGlobFlag(self,flag):
        if flag in self.globFlags:
            return True
        else:
            return False
    
    def highlight(self,player):
        self.addFlag(player,ISHIGHLIGHTED)

    def unhighlight(self,player):
        self.rmFlag(player,ISHIGHLIGHTED)

    def select(self,player):
        self.addFlag(player,ISSELECTED)

    def unselect(self,player):
        self.rmFlag(player,ISSELECTED)

    def addFog(self,player):
        self.addFlag(player,WITHFOG)

    def removeFog(self,player):
        self.rmFlag(player,WITHFOG)
    
    def draw(self,x=0,y=0,xpixoffset=0,ypixoffset=0):
        x1,y1=self.coord
        self.tile.draw(x1,y1,x,y,xpixoffset,ypixoffset,self.pos)

    def drawInfos(self,x=0,y=0,xpixoffset=0,ypixoffset=0):
        x1,y1=self.coord
        self.tile.drawInfos(x1,y1,x,y,xpixoffset,ypixoffset,self.pos,self.status[self.currentPlayer]+self.globFlags,self.passable,self.text)
    
    def drawOnMinimap(self,position):
        x,y = self.coord
        xoff,yoff = position
        self.minimapTile.draw(x,y,0,0,xoff,yoff,pos=0)

    def getPassable(self):
        return self.passable

    def setCurrentPlayer(self,player):
        self.currentPlayer = player

    def setText(self,text):
        self.text = text

    # Usefull in order to make non rectangular Map.
    # Out of board hexs are considered out of the game.
    # Nothing can be done with them.
    def setOutOfBoard(self):
        self.addGlobFlag(OUTOFBOARD)

    def unsetOutOfBoard(self):
        self.rmGlobFlag(OUTOFBOARD)

    
    
    
class emptyHex(basicHex):
    description = "Empty space"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,NE,NO,S,SE,SO],players,owner  )

class singleWallHex(basicHex):
    # WallHex
    description = "Wall"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[NE,NO,S,SE,SO],players,owner )
        self.type = SINGLEWALL
       

class doubleWallHex(basicHex):
    description = "Double wall"
    # WallHex
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[NE,NO,SE,SO],players,owner )
        self.type = DOUBLEWALL

class hallHex(basicHex):
    description = "Hall"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,NE,NO,S,SE,SO],players,owner )
        self.type = HALL

class reactorHex(basicHex):
    description = "Reactor"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,NE,NO,S,SE,SO],players,owner )
        self.type = REACTOR

class radarHex(basicHex):
    description = "Radar control center"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,NE,NO,S,SE,SO],players,owner )
        self.type = RADAR

class attacksystemHex(basicHex):
    description = "Weapons control center"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,NE,NO,S,SE,SO],players,owner )
        self.type = ATTACKCONTROL

class shieldControlHex(basicHex):
    description = "Shield control center"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,NE,NO,S,SE,SO],players,owner )
        self.type = SHIELDCONTROL

class shieldHex(basicHex):
    description = "Shield"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,NE,NO,S,SE,SO],players,owner )
        self.type = SHIELD

class cryoHex(basicHex):
    description = "Cryogenic system"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,NE,NO,S,SE,SO],players,owner )
        self.type = CRYO

class warehouseHex(basicHex):
    description = "Warehouse"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,NE,NO,S,SE,SO],players,owner )
        self.type = WAREHOUSE

class driveHex(basicHex):
    description = "Drive systems"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,NE,NO,S,SE,SO],players,owner )
        self.type = DRIVE

class fullWallHex(basicHex):
    description = "Shielded plain wall"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[],players,owner )
        self.type = FULLWALL

class weaponSlot(basicHex):
    description = "Weapon slot"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[],players,owner )
        self.type = WEAPONSLOT

class corridor(basicHex):
    description = "Corridor"
    def __init__(self,coord,pos,skin,players,owner = None ):
        basicHex.__init__(self,coord,pos,skin,[N,S],players,owner )
        self.type = CORRIDOR


id2hexConst = {"E": emptyHex,
                   "W" : singleWallHex,
                   "W2" : doubleWallHex,
                   "H" : hallHex,
                   "R" : reactorHex,
                   "A" : attacksystemHex,
                   "C" : cryoHex,
                   "D" : driveHex,
                   "RA" : radarHex,
                   "S" : shieldHex,
                   "SC" : shieldControlHex, 
                   "FW" : fullWallHex,
                   "WS" : weaponSlot,
                   "CO" : corridor}

dictType2hexConst = {EMPTY: emptyHex,
                   SINGLEWALL : singleWallHex,
                   DOUBLEWALL : doubleWallHex,
                   HALL : hallHex,
                   REACTOR : reactorHex,
                   ATTACKCONTROL : attacksystemHex,
                   CRYO : cryoHex,
                   DRIVE : driveHex,
                   RADAR : radarHex,
                   SHIELD : shieldHex,
                   SHIELDCONTROL : shieldControlHex, 
                   FULLWALL : fullWallHex,
                   WEAPONSLOT : weaponSlot,
                   CORRIDOR : corridor}



if __name__ == "__main__":
    hex1 =  fullWallHex((0,0),0,"fullwall","1")
    hex2 = hex1.clone()
    hex2.coord = (1,1)

    print hex1.coord
    print hex2.coord

    
